package com.marakana.yamba4;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.TextView;

import com.chris.weibo.R;

public class TimeLineActivity extends Activity {

	DbHelper dbHelper;
	SQLiteDatabase db;
	Cursor cursor;
	TextView textTimeLine;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_timeline_basic);

		textTimeLine = (TextView) findViewById(R.id.textTimeLine);
		/*
		 * 连接数据库
		 */
		dbHelper = new DbHelper(this);
		db = dbHelper.getReadableDatabase();
	}

	@Override
	protected void onResume() {
		super.onResume();

		// 从数据库中读取数据
		cursor = db.query(DbHelper.TABLE, null, null, null, null, null,
				DbHelper.C_CREATED_AT + " DESC");
		startManagingCursor(cursor);
		// 遍历所得的数据并输出
		String user, text, output;
		while (cursor.moveToNext()) {
			user = cursor.getString(cursor.getColumnIndex(DbHelper.C_USER));
			text = cursor.getString(cursor.getColumnIndex(DbHelper.C_TEXT));
			output = String.format("%s: %s", user, text);
			textTimeLine.append(output);
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		db.close();
	}

}
